# @Desc    : 身体健康评估系统
# @Author  : meihongliang  
# @Time    : 2025-03-01 13:04
import streamlit as st

# 设置页面配置
st.set_page_config(
    page_title="身体健康评估系统",
    layout="wide",
    initial_sidebar_state="expanded"
)


def main():
    # 页面标题
    st.title("身体健康评估系统")

    # 选择计算类型
    calc_option = st.sidebar.selectbox(
        "请选择计算类型",
        ["基础代谢率", "BMI计算", "体脂率估算"]
    )

    # 输入数据收集
    if calc_option == "输入数据":
        st.header("请输入您的身体数据")

        with st.form(key='input_form'):
            col1, col2 = st.columns(2)
            with col1:
                weight = st.number_input(label="体重 (kg)", min_value=0.0, step=0.1)
                age = st.number_input(label="年龄", min_value=0, max_value=150, step=1)
                gender = st.radio("性别", ["男", "女"])
            with col2:
                height_cm = st.number_input(label="身高 (cm)", min_value=0.0, step=0.1)
                activity_level = st.selectbox("活动水平", ["低", "中", "高"])
                target_weight = st.number_input(label="目标体重 (kg)", min_value=0.0, step=0.1)

            # 提交按钮
            submitted = st.form_submit_button("提交数据")

            if submitted:
                # 保存输入数据（这里假设我们使用简单的字典来存储）
                data = {
                    "weight": weight,
                    "height_cm": height_cm,
                    "age": age,
                    "gender": gender,
                    "activity_level": activity_level,
                    "target_weight": target_weight
                }

                # 计算 BMI
                height_m = height_cm / 100
                bmi = weight / (height_m ** 2)
                rounded_bmi = round(bmi, 1)

                # 计算基础代谢率
                if gender == "男":
                    bmr = 8.367 * float(weight) + 4.795 * float(height_cm) - 5.677 * float(age) + 5.300
                else:
                    bmr = 7.26 * float(weight) + 3.546 * float(height_cm) - 4.330 * float(age) + 5.300
                rounded_bmr = round(bmr, 1)

                # 显示计算结果
                st.subheader("计算结果")
                col1_res, col2_res = st.columns(2)
                with col1_res:
                    st.metric("BMI", f"{rounded_bmi} kg/m²")
                    st.metric("目标 BMI", f"{round(target_weight / (height_m ** 2), 1)} kg/m²")
                with col2_res:
                    st.metric("基础代谢率 (BMR)", f"{rounded_bmr} kcal/day")

                # 健康建议
                st.subheader("健康建议")
                if bmi < 18.5:
                    st.info("您的 BMI 较低，建议增加体重以改善身体状况。")
                elif 18.5 <= bmi < 24.9:
                    st.success("您的 BMI 在正常范围内，继续保持！")
                else:
                    st.warning("您的 BMI 较高，建议减少体重以促进健康。")

    # BMI计算
    elif calc_option == "BMI计算":
        st.header("BMI 计算工具")

        with st.form(key='bmi_form'):
            col1, col2 = st.columns(2)
            with col1:
                weight = st.number_input(label="体重 (kg)", min_value=0.0, step=0.1)
                height_cm = st.number_input(label="身高 (cm)", min_value=0.0, step=0.1)
            with col2:
                age = st.number_input(label="年龄", min_value=0, max_value=150, step=1)
                gender = st.radio("性别", ["男", "女"])

            submitted = st.form_submit_button("计算 BMI")

            if submitted:
                height_m = height_cm / 100
                bmi = weight / (height_m ** 2)
                rounded_bmi = round(bmi, 1)

                # 显示结果
                st.subheader(f"您的 BMI 是：{rounded_bmi}")
                if rounded_bmi < 18.5:
                    st.info("过低")
                elif 18.5 <= rounded_bmi < 24.9:
                    st.success("正常")
                else:
                    st.warning("偏高")

    # 基础代谢率计算
    elif calc_option == "基础代谢率":
        st.header("基础代谢率 (BMR) 计算工具")

        with st.form(key='bmr_form'):
            col1, col2 = st.columns(2)
            with col1:
                weight = st.number_input(label="体重 (kg)", min_value=0.0, step=0.1)
                height_cm = st.number_input(label="身高 (cm)", min_value=0.0, step=0.1)
                age = st.number_input(label="年龄", min_value=0, max_value=150, step=1)
            with col2:
                gender = st.radio("性别", ["男", "女"])
                activity_level = st.selectbox("活动水平", ["低", "中", "高"])

            submitted = st.form_submit_button("计算 BMR")

            if submitted:
                # 计算基础代谢率
                if gender == "男":
                    bmr = 8.367 * float(weight) + 4.795 * float(height_cm) - 5.677 * float(age) + 5.300
                else:
                    bmr = 7.26 * float(weight) + 3.546 * float(height_cm) - 4.330 * float(age) + 5.300
                rounded_bmr = round(bmr, 1)

                # 根据活动水平估计每日消耗
                if activity_level == "低":
                    total_calories = bmr * 1.2
                elif activity_level == "中":
                    total_calories = bmr * 1.55
                else:
                    total_calories = bmr * 1.9

                # 显示结果
                st.subheader(f"您的基础代谢率 (BMR) 是：{rounded_bmr} kcal/day")
                st.info(f"根据活动水平，您每日大约消耗 {round(total_calories, 1)} 千卡")


# 运行主函数
if __name__ == "__main__":
    main()